#include "DoxygenParser.h"

#include "DoxyFile.h"
#include "DoxyClass.h"

QPointer<IDoxygenParser> DoxygenParser::m_parser;

/**
* @brief getInstance Gets an instance of the class
* @return An instance of the class
*/
DoxygenParser::~DoxygenParser()
{
    if(m_indexLoader)
    {
        delete m_indexLoader;
    }

}

QPointer<IDoxygenParser> DoxygenParser::getInstance()
{
    if(m_parser.isNull())
    {
        m_parser = new DoxygenParser;
    }

    return m_parser;
}


/**
 * @brief loadIndexFile Loads an doxygen index file from configuration, will remove any previously loaded index file
 * @param fileName The index file to load
 * @return True if the file has been loaded sucessfuly.
 */
bool DoxygenParser::loadIndexFile(const QString fileName)
{
    bool returnCon = false;

    //if the index file already exsists delete it as we will create a new one
    if(m_indexLoader)
    {
        delete m_indexLoader;
    }

    m_indexLoader = new IndexLoader(fileName);

    if(m_indexLoader)
    {
        int pos = fileName.lastIndexOf("/");
        m_currentWorkingPath = fileName.left(pos);
       returnCon =  m_indexLoader->loadFile();
    }
    return returnCon;
}




/**
* @brief getFileNames Poulates the given string list with all of the files that have been processed via doxygen
* @param FileNames A list of the file names that have been proccessed.
*/
void DoxygenParser::getFileNames(QList<RefItem> &FileNames) const
{
    FileNames.clear();

    if(m_indexLoader)
    {
      m_indexLoader->getFileNames(FileNames);
    }
}

void DoxygenParser::getClassNames(QList<RefItem> &ClassNames) const
{
    ClassNames.clear();

    if(m_indexLoader)
    {
      m_indexLoader->getClassNames(ClassNames);
    }
}

const IDoxyFile *DoxygenParser::loadFile(QString refId)
{
    DoxyFile* file;
    QString filename = m_currentWorkingPath;
    filename.append("/");
    filename.append(refId);
    if(m_indexLoader)
    {
        //check that the refid is valid
        if(m_indexLoader->validFileRef(refId))
        {
            //remove the current file
            if(m_doxyFile)
            {
                delete m_doxyFile;
            }

            file = new DoxyFile(filename);

            if(file->loadFile())
            {
                m_doxyFile = file;
            }
            else
            {
                delete file;
            }

        }
    }

    return file;
}

/**
 * @brief loadClass Load a class processed by doxygen. Note that only one class may be loaded at a time. Another load will cause the previous class to be disposed of
 * @param refId The refrence id of the class to load
 * @return A doxy representation of a class that has been processed, if NUll is returned a file has failed to be loaded.
 */
const IDoxyClass *DoxygenParser::loadClass(QString refId)
{
    DoxyClass* doxClass;

    QString filename = m_currentWorkingPath;
    filename.append("/");
    filename.append(refId);

    if(m_indexLoader)
    {
        if(m_indexLoader->validClassRef(refId))
        {
            if(m_doxyClass)
            {
                delete m_doxyClass;
            }
            doxClass = new DoxyClass(filename);

            if(doxClass->loadFile())
            {
                m_doxyClass = doxClass;
            }
            else
            {
                delete doxClass;
            }
        }
    }

   return doxClass;
}



/************************************ Private ****************************/
/**
 * @brief DoxygenParser::DoxygenParser
 */
DoxygenParser::DoxygenParser():
    m_indexLoader(NULL),
    m_doxyFile(NULL),
    m_doxyClass(NULL)
{
}
